'use strict';

const Controller = require('egg').Controller

class MainController extends Controller {

    async index() {
        //首页的文章列表数据
        this.ctx.body = 'hi api'
    }

    async checkLogin() {
        let userName = this.ctx.request.body.userName;
        let password = this.ctx.request.body.password;

        const sql = " SELECT userName FROM admin_user WHERE userName = '" + userName +
            "' AND password = '" + password + "'"

        const res = await this.app.mysql.query(sql);
        const token = this.app.jwt.sign({
            name: userName,
        }, this.app.config.jwt.secret);

        if (res.length > 0) {
            //登录成功,进行session缓存
            this.ctx.body = { "code": 200, 'data': '登录成功', token }
        } else {
            this.ctx.body = { "code": 100, data: '登录失败' }
        }
    }

    //后台文章分类信息
    async getTypeInfo() {
        const resType = await this.app.mysql.select('type')
        this.ctx.body = { "code": 200, data: resType }
    }

    //添加文章
    async addArticle() {
        let tmpArticle = this.ctx.request.body;
        const result = await this.app.mysql.insert('article', tmpArticle)
        const insertSuccess = result.affectedRows === 1
        const insertId = result.insertId

        if (insertSuccess) {
            this.ctx.body = {
                code: 200,
                msg: "添加成功",
                insertId: insertId
            }
        } else {
            this.ctx.body = {
                code: 100,
                msg: "添加失败",
            }
        }
    }

    //修改文章
    async updateArticle() {
        let tmpArticle = this.ctx.request.body;
        const result = await this.app.mysql.update('article', tmpArticle)
        const updateSuccess = result.affectedRows === 1

        if (updateSuccess) {
            this.ctx.body = {
                code: 200,
                msg: "修改成功",
            }
        } else {
            this.ctx.body = {
                code: 100,
                msg: "修改失败",
            }
        }
    }

    //获得文章列表
    async getArticleList() {

        let sql = 'SELECT article.id as id,' +
            'article.title as title,' +
            'article.introduce as introduce,' +
            'article.view_count as viewCount,' +
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime," +
            'type.typeName as typeName ' +
            'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
            'ORDER BY article.id DESC '

        const resList = await this.app.mysql.query(sql)
        const data = {
            code: 200,
            data: resList
        }
        this.ctx.body = data
    }

    //删除文章
    async delArticle() {
        let id = this.ctx.params.id
        const res = await this.app.mysql.delete('article', { 'id': id })
        this.ctx.body = { code: 200, data: res }
    }

    //根据文章ID得到文章详情，用于修改文章
    async getArticleById() {
        let id = this.ctx.params.id
        let sql = 'SELECT article.id as id,' +
            'article.title as title,' +
            'article.introduce as introduce,' +
            'article.article_cointent as article_cointent,' +
            "FROM_UNIXTIME(article.addTime,'%Y-%m-%d' ) as addTime," +
            'article.view_count as view_count ,' +
            'type.typeName as typeName ,' +
            'type.id as typeId ' +
            'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
            'WHERE article.id=' + id
        const result = await this.app.mysql.query(sql)
        this.ctx.body = { code:200 , data: result }

    }

}

module.exports = MainController